﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MosCraft.Utilities.NoiseGenerators
{
    public class NoiseGeneratorOctaves : NoiseGenerator
    {
        private NoiseGeneratorPerlin[] generatorCollection;
        private int frequency;

        public NoiseGeneratorOctaves(FastRandom random, int frequency)
        {
            this.frequency = frequency;
            generatorCollection = new NoiseGeneratorPerlin[frequency];

            for (int j = 0; j < frequency; j++)
            {
                generatorCollection[j] = new NoiseGeneratorPerlin(random);
            }
        }

        public double generate2DNoiseOctave(double x, double y)
        {
            double d2 = 0.0D;
            double d3 = 1.0D;
            for(int i = 0; i < frequency; i++)
            {
                d2 += generatorCollection[i].generateNoise2D(x * d3, y * d3) / d3;
                d3 /= 2D;
            }

            return d2;
        }

        public double[] generateNoiseOctaves(double[] noise, double x, double y, double z, int i, int j, int k, double d3, double d4, double d5)
        {
            if (noise == null)
            {
                noise = new double[i * j * k];
            } else
            {
                for (int l = 0; l < noise.Length; l++)
                {
                    noise[l] = 0.0D;
                }
            }
            double d6 = 1.0D;

            for (int i1 = 0; i1 < frequency; i1++)
            {
                generatorCollection[i1].generateNoisePerlin(noise, x, y, z, i, j, k, d3 * d6, d4 * d6, d5 * d6, d6);
                d6 /= 2D;
            }

            return noise;
        }

        public double[] generateNormalizedOctaves(double[] noise, int i, int j, int k, int l, double d, double d1, double d2)
        {
            return generateNoiseOctaves(noise, i, 10D, j, k, 1, l, d, 1.0D, d1);
        }

    }
}
